package com.vca.front.controller;

import cn.hutool.core.collection.CollUtil;
import com.vca.common.page.CommonPage;
import com.vca.common.request.CartRequest;
import com.vca.common.request.CartResetRequest;
import com.vca.common.request.PageParamRequest;
import com.vca.common.response.CartInfoResponse;
import com.vca.common.result.CommonResult;
import com.vca.service.service.StoreCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 购物车 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/front/cart")
@Api(tags = "商品 -- 购物车") //配合swagger使用
public class CartController {

    @Autowired
    private StoreCartService storeCartService;

    /**
     * 分页显示购物车表
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="isValid", value="类型，1-有效商品，0-无效商品 2 全部",required = true),
            @ApiImplicitParam(name="page", value="页码", required = true),
            @ApiImplicitParam(name="limit", value="每页数量", required = true)
    })
    public CommonResult<CommonPage<CartInfoResponse>> getList(@RequestHeader(value = "language",required = false) String language,@RequestParam Integer isValid, @Validated PageParamRequest pageParamRequest) {
        CommonPage<CartInfoResponse> restPage = CommonPage.restPage(storeCartService.getList(language,pageParamRequest, isValid));
        return CommonResult.success(restPage);
    }

    /**
    * @Description:添加购物车
    * @Author: chenBing
    * @Date: 2022/11/18
    */
    @ApiOperation(value = "添加购物车")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<HashMap<String,String>> save(@RequestHeader(value = "language",required = false) String language,@RequestBody @Validated CartRequest storeCartRequest) {
        HashMap<String, String> map = storeCartService.saveCate(language,storeCartRequest);
        if (CollUtil.isNotEmpty(map)) {
            return CommonResult.success(map);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除购物车表
     * @param ids 购物车ids
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<String> delete(@RequestParam(value = "ids") List<Long> ids) {
        if (storeCartService.deleteCartByIds(ids)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改商品数量
     * @param id integer id
     * @param number 修改的产品数量
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/num", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id, @RequestParam Integer number) {
        if (storeCartService.updateCartNum(id, number)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 获取购物车数量
     */
//    @ApiOperation(value = "获取购物车数量")
//    @RequestMapping(value = "/count", method = RequestMethod.GET)
//    public CommonResult<Map<String, Integer>> count(@Validated CartNumRequest request) {
//        return CommonResult.success(storeCartService.getUserCount(request));
//    }

    /**
     * 购物车重选提交
     * @param resetRequest 重选参数
     * @return 结果
     */
//    @ApiOperation(value = "购物车重选提交")
//    @RequestMapping(value = "/resetcart", method = RequestMethod.POST)
//    public CommonResult<Object> resetCart(@RequestBody @Validated CartResetRequest resetRequest){
//        return CommonResult.success(storeCartService.resetCart(resetRequest));
//    }
}



